<template>
  <div>
    <el-dialog
      title="提示"
      :visible.sync="dialogMedicines"
      width="30%"
      :before-close="handleClose"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="药名" prop="drugName">
          <el-input v-model="ruleForm.drugName" placeholder="请输入药名"></el-input>
        </el-form-item>
        <el-form-item label="用药时间" prop="isMedicationTime">
          <el-select
            v-model="ruleForm.isMedicationTime"
            placeholder="请输入用药时间"
            style="width: 100%"
          >
            <el-option
              v-for="(item, index) in dict.type.user_drug_date"
              :key="index"
              :label="item.label"
              :value="item.value"
              :disabled="item.disabled"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="服药依从性" prop="medicationDuration">
          <el-select
            v-model="ruleForm.medicationDuration"
            placeholder="请选择服药依从性"
            style="width: 100%"
          >
            <el-option
              v-for="(item, index) in dict.type.user_drug_rely"
              :key="index"
              :label="item.label"
              :value="item.value"
              :disabled="item.disabled"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="每日用量" prop="dailyDosage">
          <el-select
            v-model="ruleForm.dailyDosage"
            placeholder="请选择每日用量"
            style="width: 100%"
          >
            <el-option
              v-for="(item, index) in dict.type.user_drug_day"
              :key="index"
              :label="item.label"
              :value="item.value"
              :disabled="item.disabled"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addmedication,updatemedication } from "@/api/cluster/condition";
export default {
  props: ["dialogMedicines", "oldManId","ruleForm"],
  dicts: [
    "user_drug_date",
    "user_drug_rely",
    "user_drug_day",
    
  ],
  data() {
    return {
      dialogVisible: false,
      isMedicationTimeList: [
        {
          label: "早",
          value: 1,
        },
        {
          label: "中",
          value: 2,
        },
        {
          label: "晚",
          value: 3,
        },
      ],
      medicationDurationList: [
        {
          label: "规律",
          value: 1,
        },
        {
          label: "间断",
          value: 2,
        },
        {
          label: "不服药",
          value: 3,
        },
      ],
      dailyDosageList: [
        {
          label: "一日三次",
          value: 1,
        },
        {
          label: "一日两次",
          value: 2,
        },
        {
          label: "一日一次",
          value: 3,
        },
      ],

      rules: {
        drugName: [
          {
            required: true,
            message: "请输入药名",
            trigger: "change",
          },
        ],
        isMedicationTime: [
          {
            required: true,
            message: "用药时间",
            trigger: "change",
          },
        ],
        medicationDuration: [
          {
            required: true,
            message: "请选择服药依从性",
            trigger: "change",
          },
        ],
        dailyDosage: [
          {
            required: true,
            message: "请选择每日用量",
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    handleClose() {
      this.$emit("onclises", false);
      this.$refs["ruleForm"].resetFields();
      this.ruleForm = {};
    },
    handleSubmit() {
      if (this.ruleForm.id) {
        this.$refs["ruleForm"].validate((valid) => {
          if (valid) {
            this.ruleForm.oldManId = this.oldManId;
            updatemedication(this.ruleForm).then((res) => {
              if (res.code == 200) {
                this.handleClose();
              }
            });
          }
        });
      } else {
        this.$refs["ruleForm"].validate((valid) => {
          if (valid) {
            this.ruleForm.oldManId = this.oldManId;
            addmedication(this.ruleForm).then((res) => {
              if (res.code == 200) {
                this.handleClose();
              }
            });
          }
        });
      }
    },
  },
};
</script>
